package top.minuy.structure.hash;

import top.minuy.res.book.BookRes;
import top.minuy.util.helper.FileHelper;
import top.minuy.structure.linear.array.Array;
import top.minuy.structure.map.AVLTreeMap;
import top.minuy.structure.map.LinkedListMap;

/**
 * 散列表测试类
 *
 * @author Minuy
 * @time 13:43
 * @date 2021/11/7
 */
public class Test {
    public static void main(String[] args) {
        double time = 0;

        System.out.println("Test AVLTreeHashTable : ");
        AVLTreeHashTable<String, Integer> avlTreeHashTable = new AVLTreeHashTable<>();
        time = testHashTable(avlTreeHashTable);
        System.out.println("Time = " + time + " s");

        System.out.println("================================");

        System.out.println("Test AVLTreeMap : ");
        AVLTreeMap<String, Integer> avlTreeMap = new AVLTreeMap<>();
        time = top.minuy.structure.map.Test.testWRMain(avlTreeMap);
        System.out.println("Time = " + time + " s");

        System.out.println("================================");

        System.out.println("Test LinkedListHashTable : ");
        LinkedListHashTable<String, Integer> linkedListHashTable = new LinkedListHashTable<>();
        time = testHashTable(linkedListHashTable);
        System.out.println("Time = " + time + " s");

        System.out.println("================================");

        System.out.println("Test LinkedListMap : ");
        LinkedListMap<String, Integer> linkedListMap = new LinkedListMap<>();
        time = top.minuy.structure.map.Test.testWRMain(linkedListMap);
        System.out.println("Time = " + time + " s");

        System.out.println("================================");



    }

    private static double testHashTable(HashTable<String, Integer> hashTable) {
        Array<String> words = getWords(BookRes.PRIDE_AND_PREJUDICE);

        long startTime = System.nanoTime();
        // 加入
        for (int i = 0; i < words.getSize(); i++) {
            String word = words.get(i);
            if (hashTable.contains(word)) {
                hashTable.set(word, hashTable.get(word) + 1);
            } else {
                hashTable.add(word, 1);
            }
        }
        long endTime = System.nanoTime();
        System.out.println();

        System.out.println("Total word : " + words.getSize());
        System.out.println("Total different word : " + hashTable.getSize());
        System.out.println("The frequency of PRIDE : " + hashTable.get("pride"));
        System.out.println("The frequency of PREJUDICE : " + hashTable.get("prejudice"));
        System.out.println();

        return (endTime - startTime) / 1000000000.0;
    }

    private static Array<String> getWords(String fileName) {
        Array<String> words = new Array<>();
        FileHelper.readFile(fileName, words);
        return words;
    }
}
